package top.drinyeit.weilong.model

import android.view.View
import android.widget.Toast
import top.drinyeit.weilong.adapter.CartAdapter
import top.drinyeit.weilong.retrofit.HttpResponse
import top.drinyeit.weilong.retrofit.RetrofitMethod
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener
import top.drinyeit.weilong.viewmodel.CartVM.*

/**
 * Created by YJY on 2018/1/10.
 */

data class CartBean(var commodity_description: String,
                    var commodity_price: Double,
                    var commodity_picture: String,
                    var commodity_sum: Int,
                    var id: Int,
                    var commodity_name: String,
                    var store_name: String,
                    var store_id: Int,
                    var position: Int,
                    var type: Int,
                    var checked: Boolean = false) : Cloneable {

    fun getShopName(): String {
        return "【${store_name}】"
    }
    fun checkedChanged(view: View) {
        checked = !checked
        var i = cartAdapter.data.indexOf(this@CartBean)
        if (type == 1) {    //header
            i++
            while (i <= (cartAdapter.data.size - 1) && cartAdapter.data[i].position == position) {
                cartAdapter.data[i].checked = checked
                i++
            }

        } else {    //content
            while (i >= 0 && cartAdapter.data[i].position == position) {  //找到header位置
                i--
            }
            i++ //header位置
            if (!checked) {  //取消选中
                if (cartAdapter.data[i].checked) {  //店铺取消选中
                    cartAdapter.data[i].checked = false
                }
            } else {    //选中
                if (shopGoodsAllChecked(i)) {   //商品都选中则header选中
                    cartAdapter.data[i].checked = true
                }
            }
        }
        cartAdapter.notifyDataSetChanged()
        if (!checked && allChecked.get()) { //取消选中
            allChecked.set(false)
        } else {    //选中
            checkedIsAllChecked()
        }
        calculatePriceAndNumber()
    }

    private fun shopGoodsAllChecked(headPosition: Int): Boolean {
        var j = headPosition + 1
        while (j <= (cartAdapter.data.size - 1) && cartAdapter.data[j].position == position) {
            if (!cartAdapter.data[j].checked) {
                return false
            }
            j++
        }
        return true
    }

    private fun checkedIsAllChecked() {
        for (cartBean in cartAdapter.data) {
            if (cartBean.type == 1 && (!cartBean.checked)) {
                return
            }
        }
        allChecked.set(true)
    }
    fun subtract(view: View) {
        if (commodity_sum > 1) {
            val subscriber = CommonSubscriber<HttpResponse<String>>(object : SubscriberListener<HttpResponse<String>> {
                override fun onError(e: String?, code: Int) {
                    Toast.makeText(view.context, "网络繁忙", Toast.LENGTH_SHORT).show()
                }

                override fun onNext(t: HttpResponse<String>?) {
                    commodity_sum--
                    cartAdapter.notifyDataSetChanged()
                    calculatePriceAndNumber()
                }
            })
            RetrofitMethod.getInstance().changeCartNumber(subscriber, id.toString(), commodity_sum - 1)
        }
    }


    fun add(view: View) {
        val subscriber = CommonSubscriber<HttpResponse<String>>(object : SubscriberListener<HttpResponse<String>> {
            override fun onError(e: String?, code: Int) {
                Toast.makeText(view.context, "网络繁忙", Toast.LENGTH_SHORT).show()
            }

            override fun onNext(t: HttpResponse<String>?) {
                commodity_sum++
                cartAdapter.notifyDataSetChanged()
                calculatePriceAndNumber()
            }
        })
        RetrofitMethod.getInstance().changeCartNumber(subscriber, id.toString(), commodity_sum + 1)
    }

    @Throws(CloneNotSupportedException::class)
    public override fun clone(): Any {
        val cartBean: CartBean?
        cartBean = super.clone() as CartBean
        return cartBean
    }
}